package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocal;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplatete;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 用户点赞行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.参数校验
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取登录用户
        ApUser apUser = ApThreadLocal.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if (apUser.getId() == 0) {
            return ResponseResult.okResult(405, "游客身份禁止，需进行登录");
        }
        //3.查询当前用户是否点赞
        //定义redis上的key
        String likes_key = "USER_LIKES" + dto.getArticleId();
        Boolean flag = redisTemplatete.opsForHash().hasKey(likes_key, apUser.getId() + "");
        //如果已经点赞，并且正在执行点赞动作 直接返回
        if (flag && dto.getOperation() == 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (flag && dto.getOperation() == 1) {
            //删除redis
            redisTemplatete.opsForHash().delete(likes_key, apUser.getId() + "");

            //删除mongo
            mongoTemplate.remove(
                    Query.query(Criteria.where("userId").is(apUser.getId())
                    .and("articleId").is(dto.getArticleId()))
                    , ApLikesBehavior.class);

            return ResponseResult.okResult("取消点赞成功");
        }
        //如果没有点赞过，则在执行点赞的时候，添加redis mongo
        if (!flag && dto.getOperation() == 0) {
            //添加redis
            redisTemplatete.opsForHash().put(likes_key, apUser.getId() + "", "1");
            //添加mongo
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setId(ObjectId.get().toHexString());
            apLikesBehavior.setType((short) 0);
            apLikesBehavior.setUserId(apUser.getId().longValue());
            mongoTemplate.insert(apLikesBehavior);
            return ResponseResult.okResult("点赞成功");
        }
        return null;
    }
}
